package ngocthien.game;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.handler.timer.ITimerCallback;
import org.anddev.andengine.engine.handler.timer.TimerHandler;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.FillResolutionPolicy;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.IEntity;
import org.anddev.andengine.entity.modifier.FadeInModifier;
import org.anddev.andengine.entity.modifier.FadeOutModifier;
import org.anddev.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.scene.menu.MenuScene;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import org.anddev.andengine.util.modifier.IModifier;
import org.anddev.andengine.util.modifier.ease.EaseLinear;

import android.content.Intent;

public class StateSplash extends BaseGameActivity{
	
	

	
	protected MenuScene mMenuScene;
	
	
	protected TextureRegion mMenuResetTextureRegion;
	protected TextureRegion mMenuQuitTextureRegion;
	
	private Sprite splash;
	private Camera mCamera;
	private BitmapTextureAtlas mBackgroundTexture;
	private Font mFont;
	private Scene mMainScene;
	private TextureRegion mBackgroundTextureRegion;
	

	public Engine onLoadEngine() {
		// TODO Auto-generated method stub

		mCamera=new Camera(0, 0, Global.CAMERA_WIDTH, Global.CAMERA_HEIGHT);
		return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new FillResolutionPolicy(), mCamera));
	}

	public void onLoadResources() {
		// TODO Auto-generated method stub
		
		mBackgroundTexture=new BitmapTextureAtlas(256, 128, TextureOptions.DEFAULT);
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		mBackgroundTextureRegion=BitmapTextureAtlasTextureRegionFactory.createFromAsset(mBackgroundTexture, this, "splash.png", 0, 0);
		this.mEngine.getTextureManager().loadTexture(mBackgroundTexture);
	}
	public Scene onLoadScene() {
		// TODO Auto-generated method stub
		
		this.mEngine.registerUpdateHandler(new FPSLogger());
		
		mMainScene=new Scene();
		mMainScene.setBackground(new ColorBackground(1f, 1f, 1f));
		
		final int centerX=(Global.CAMERA_WIDTH-mBackgroundTextureRegion.getWidth())/2;
		final int centerY=(Global.CAMERA_HEIGHT-mBackgroundTextureRegion.getHeight())/2;
		
		
		splash=new Sprite(centerX, centerY, mBackgroundTextureRegion);
		
		FadeInModifier fadeinModifier=new FadeInModifier(2f,new IEntityModifierListener() {
		
			public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem) {
				// TODO Auto-generated method stub
			}
			
			public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
				// TODO Auto-generated method stub
				createSpriteSpawnTimeHandler();
				
			}
		},EaseLinear.getInstance());
		
		splash.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		splash.registerEntityModifier(fadeinModifier);
		
		splash.setScale(1.5f);
		mMainScene.attachChild(splash);
		return mMainScene;
	}
	
	private void createSpriteSpawnTimeHandler(){
		
		this.getEngine().registerUpdateHandler(new TimerHandler(0.5f,true, new ITimerCallback() {
			
			public void onTimePassed(TimerHandler pTimerHandler) {
				// TODO Auto-generated method stub
				
				FadeOutModifier fadeoutModifier=new FadeOutModifier(2f, new IEntityModifierListener() {
					
					public void onModifierStarted(IModifier<IEntity> pModifier, IEntity pItem) {
						// TODO Auto-generated method stub
					}
					
					public void onModifierFinished(IModifier<IEntity> pModifier, IEntity pItem) {
						// TODO Auto-generated method stub
						
						Intent intent=new Intent(StateSplash.this, StateMenu.class);
						startActivity(intent);
						finish();
					}
				},EaseLinear.getInstance());
				
        		splash.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        		splash.registerEntityModifier(fadeoutModifier);
			}
		}));
	}

	public void onLoadComplete() {
		// TODO Auto-generated method stub
		
	}

}
